package com.db.logging;

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author nulls.narik
 */
public class MyLoggerFactory implements ILoggerFactory {

    private final Map<String, Logger> loggers;

    private final Object lockAdd = new Object();

    public MyLoggerFactory() {
        loggers = new HashMap<String, Logger>();
        loggers.put(Logger.ROOT_LOGGER_NAME, new MyConsoleLogger(Logger.ROOT_LOGGER_NAME));
    }

    @Override
    public Logger getLogger(String s) {
        if (s.isEmpty())
            throw new IllegalArgumentException();

        synchronized (lockAdd) {
            Logger logger = null;
            try {
                logger = loggers.get(s);
                if (logger != null)
                    return logger;
                logger = new MyFileLogger(s, MyFileLogger.Level.INFO);
                loggers.put(s, logger);
            } catch (FileNotFoundException e) {
                System.out.println("Error create file logger: file not found");
            }
            return logger;
        }
    }
}
